package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.Emp;
import com.itheima.pojo.PageBean;
import com.itheima.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class EmpServiceImpl implements EmpService {

    @Autowired
    private EmpMapper empMapper;

    @Override
    public PageBean page(Integer page, Integer pageSize, String name,
                         Short gender, LocalDate begin, LocalDate end) {
        //1. 开启分页插件
        PageHelper.startPage(page, pageSize);

        //2. 查询员工数据，无需手动分页
        Page<Emp> pageInfo = empMapper.page(name, gender, begin, end);

        //3.封装返回
        return new PageBean(pageInfo.getTotal(), pageInfo.getResult());
    }

    @Override
    public void save(Emp emp) {
        //补全数据
        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(LocalDateTime.now());
        //调用添加方法
        empMapper.insert(emp);
    }

    @Override
    public void delete(List<Integer> ids) {
        empMapper.delete(ids);
    }

    /**
     * 根据ID查询员工
     *
     * @param id
     * @return
     */
    @Override
    public Emp getById(Integer id) {
        return empMapper.findById(id);
    }

    @Override
    public void update(Emp emp) {
        emp.setUpdateTime(LocalDateTime.now()); //更新修改时间为当前时间

        empMapper.update(emp);
    }

    /**
     * 根据用户名和密码查询员工
     *
     * @param username
     * @param password
     * @return
     */
    @Override
    public Emp findByUserNameAndPassword(String username, String password) {
        return empMapper.findByUserNameAndPassword(username, password);
    }
}